package com;

import java.awt.*;

/**
 * 弹弓
 */
public class Slingshot {
    private static final int slingShotMaxLength = 150;
    private int x = 150;
    private int y = 190;
    private int focusX = 160;
    private int focusY = 195;

    private World world;

    private HeroBird bird;
    private boolean shooting = false;

    public void paintImage(Graphics2D g){
        g.setColor(Color.BLACK);
        Images.slingshot.paintIcon(null,g,x,World.HEIGHT-y);
        Stroke stroke = g.getStroke();
        g.setStroke(new BasicStroke(5));
        if(!shooting) {
            g.drawLine(focusX + 10, World.HEIGHT - focusY + 25, bird.getBody().getPixelX() + 5, bird.getBody().getPixelY() + 28);
            g.drawLine(focusX + 30, World.HEIGHT - focusY + 25, bird.getBody().getPixelX() + 5, bird.getBody().getPixelY() + 28);

        }else{
            g.drawLine(focusX + 10, World.HEIGHT - focusY + 25, focusX + 30, World.HEIGHT - focusY + 25);
        }
        g.setStroke(stroke);
    }

    public HeroBird reLoading(){
        bird = new HeroBird(focusX,focusY);
        shooting = false;
        return bird;
    }

    /**
     * 移动小鸟位置
     * @param x
     * @param y
     */
    public void moveHeroBird(int x,int y){
        double len = Math.sqrt(Math.pow(focusX-x,2)+Math.pow(focusY-y,2));
        if(len<=slingShotMaxLength){
            bird.getBody().setCenterPosition(x,y);
        }else{
            double radians = Math.atan2(y-focusY,x-focusX);
            x = (int)(Math.cos(radians)*slingShotMaxLength+focusX);
            y = (int)(Math.sin(radians)*slingShotMaxLength+focusY);
            bird.getBody().setCenterPosition(x,y);
        }

    }

    public void shoot(){
        //如果小鸟距离弹弓较近，则不发射
        double len = Math.sqrt(Math.pow(focusX-bird.getBody().getPixelX(),2)+Math.pow(focusY-bird.getBody().getPixelX(),2));
        if(len<50){
            return;
        }

        shooting = true;
        bird.go((focusX-bird.getBody().getPixelX())*5,(focusY-World.HEIGHT+bird.getBody().getPixelY())*5);
    }

    public boolean isShooting() {
        return shooting;
    }

    public static void main(String[] args) {
        System.out.println(Math.pow(3,2));
    }
}
